package com.ald.crossex.action.user;

import com.ald.crossex.dto.*;
import com.ald.crossex.entity.TCeCoreResource;
import com.ald.crossex.entity.TCeCoreRole;
import com.ald.crossex.service.ResourceService;
import com.ald.crossex.service.RoleService;
import com.ald.crossex.util.Page;
import com.ald.crossex.utils.PageUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by lishijiong on 15/4/3.
 */
@Controller
@RequestMapping(value = "admin/auth")
public class SystemAction {

    @Autowired
    private ResourceService resourceService;
    @Autowired
    private RoleService roleService;

    private Logger logger = Logger.getLogger(this.getClass());

    @RequestMapping(value = "/roleManage")
    public String roleManage() {
        return "admin/auth/roleManage";
    }

    @RequestMapping(value = "/resourceManage")
    public String resourceManage() {
        return "admin/auth/resourceManage";
    }

    /**
     *  获取资源列表
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/resource/list")
    public JqGridResponse getFirstLevelResource(@RequestBody JqGridRequest request) {
        Page page = resourceService.listFirstLevelResource(request);
        return PageUtils.pageToJqGridResponse(page);
    }

    /**
     * 获取资源树形结构
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/resource/tree")
    public List<ResourceDto> getResourceTree(){
        return resourceService.getResourceTree();
    }

    /**
     * 根据父id获取资源列表
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/resource/listByParentId")
    public JqGridResponse getResourceByParentId(JqGridRequest request){
        Page page = resourceService.listResourceByParentId(request);
        return PageUtils.pageToJqGridResponse(page);

    }

    /**
     * 获取角色列表
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/role/list")
    public JqGridResponse listRole(@RequestBody JqGridRequest request) {

        Page page = roleService.listRole(request);
        return PageUtils.pageToJqGridResponse(page);

    }

    /**
     * 新增、修改、删除资源
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/resource/edit", method = RequestMethod.POST)
    public JqGridResponse editResource(EditResourceRequest request) throws Exception {
        String oper = request.getOper();
        try {
            if (oper.equals(JqGridRequest.JQGRIDREQUEST_OPER_ADD)) {
                TCeCoreResource res = new TCeCoreResource();
                res.setName(request.getName());
                res.setSort(request.getSort());
                res.setUrl(request.getUrl());
                res.setIconClass(request.getIconClass());
                res.setType(request.getType());
                res.setRemark(request.getRemark());
                res.setParentId(Integer.parseInt(request.getParent()));
                resourceService.addResource(res);
            } else if (oper.equals(JqGridRequest.JQGRIDREQUEST_OPER_EDIT)) {
                TCeCoreResource res = new TCeCoreResource();
                res.setName(request.getName());
                res.setSort(request.getSort());
                res.setUrl(request.getUrl());
                res.setIconClass(request.getIconClass());
                res.setType(request.getType());
                res.setRemark(request.getRemark());
                res.setRid(request.getRid());
                res.setParentId(Integer.parseInt(request.getParent()));
                resourceService.updateResource(res);
            } else if (oper.equals(JqGridRequest.JQGRIDREQUEST_OPER_DEL)) {
                if (resourceService.delResource(request.getId()) <= 0) {
                    return new JqGridResponse(JqGridResponse.JQGRID_STATUS_ERROR);
                }
            }
        } catch (Exception e) {
            return new JqGridResponse(JqGridResponse.JQGRID_STATUS_ERROR);
        }

        return new JqGridResponse(JqGridResponse.JQGRID_STATUS_SUCCESS);
    }

    /**
     * 新增、修改、删除角色
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/role/edit", method = RequestMethod.POST)
    public JqGridResponse editRole(EditRoleRequest request) throws Exception {
        String oper = request.getOper();
        TCeCoreRole role = new TCeCoreRole();
        role.setRoleName(request.getRoleName());
        role.setRemark(request.getRemark());
        role.setOrgId(request.getOrgId());
        role.setBuiltin(request.getBuiltin());
        try {
            if (oper.equals(JqGridRequest.JQGRIDREQUEST_OPER_ADD)) {
                roleService.addRole(role);
            } else if (oper.equals(JqGridRequest.JQGRIDREQUEST_OPER_EDIT)) {
                role.setRid(request.getRid());
                roleService.updateRole(role);
            } else if (oper.equals(JqGridRequest.JQGRIDREQUEST_OPER_DEL)) {
                if (roleService.delRole(request.getId()) <= 0) {
                    return new JqGridResponse(JqGridResponse.JQGRID_STATUS_ERROR);
                }
            }
        } catch (Exception e) {
            return new JqGridResponse(JqGridResponse.JQGRID_STATUS_ERROR);
        }

        return new JqGridResponse(JqGridResponse.JQGRID_STATUS_SUCCESS);
    }


}
